import mongoengine as db

from app.db import BaseDocument, BaseEmbeddedDocument
from app.db.models.meta_limit import (
    TypeLimit,
    TypeFreq,
    TypePeriod,
    TypeDuration,
    ExpTypeLimit
)


class Feature(BaseEmbeddedDocument):
    httpTask = db.BooleanField(default=False)
    pingTask = db.BooleanField(default=False)
    vulTask = db.BooleanField(default=False)
    vulManualTask = db.BooleanField(default=False)
    sslTask = db.BooleanField(default=False)
    sslManualTask = db.BooleanField(default=False)
    securityEventTask = db.BooleanField(default=False)
    securityEventManualTask = db.BooleanField(default=False)
    assetTask = db.BooleanField(default=False)
    assetManualTask = db.BooleanField(default=False)
    smsAlert = db.BooleanField(default=False)
    emailAlert = db.BooleanField(default=False)
    reportDownload = db.BooleanField(default=False)
    contentTask = db.BooleanField(default=False)
    contentManualTask = db.BooleanField(default=False)
    hostVulTask = db.BooleanField(default=False)
    hostVulManualTask = db.BooleanField(default=False)
    ipv6ManualTask = db.BooleanField(default=False)
    # 对外API服务
    api = db.BooleanField(default=False)
    # 扫描时间控制
    wsTaskTime = db.BooleanField(default=False)
    # 篡改监测任务， 篡改监测只支持周期任务
    changeCheckTask = db.BooleanField(default=False)


class Balance(BaseEmbeddedDocument):
    domain = db.EmbeddedDocumentField(TypeLimit, default=TypeLimit)
    domainEnable = db.EmbeddedDocumentField(TypeLimit, default=TypeLimit)

    # http
    httpMaxTask = db.EmbeddedDocumentField(TypeLimit, default=TypeLimit)
    httpMaxTaskEnable = db.EmbeddedDocumentField(TypeLimit, default=TypeLimit)
    httpMaxFreq = db.EmbeddedDocumentField(TypeFreq, default=TypeFreq)
    httpMaxNode = db.EmbeddedDocumentField(TypeFreq, default=TypeFreq)

    # ping
    pingMaxTask = db.EmbeddedDocumentField(TypeLimit, default=TypeLimit)
    pingMaxTaskEnable = db.EmbeddedDocumentField(TypeLimit, default=TypeLimit)
    pingMaxFreq = db.EmbeddedDocumentField(TypeFreq, default=TypeFreq)
    pingMaxNode = db.EmbeddedDocumentField(TypeFreq, default=TypeFreq)

    # vul
    vulManualTask = db.EmbeddedDocumentField(TypePeriod, default=TypePeriod)
    vulMaxFreq = db.EmbeddedDocumentField(TypeFreq, default=TypeFreq)
    vulMaxDepth = db.EmbeddedDocumentField(TypeFreq, default=TypeFreq)
    vulMaxPage = db.EmbeddedDocumentField(TypeFreq, default=TypeFreq)

    # sitePortrait
    sitePortrait = db.EmbeddedDocumentField(TypeLimit, default=TypeLimit)

    # ssl
    sslMaxFreq = db.EmbeddedDocumentField(TypeFreq, default=TypeFreq)
    sslManualTask = db.EmbeddedDocumentField(TypePeriod, default=TypePeriod)

    # securityEvent
    securityEventMaxFreq = db.EmbeddedDocumentField(TypeFreq, default=TypeFreq)
    securityEventManualTask = db.EmbeddedDocumentField(TypePeriod, default=TypePeriod)
    securityEventMaxDepth = db.EmbeddedDocumentField(TypeFreq, default=TypeFreq)
    securityEventMaxPage = db.EmbeddedDocumentField(TypeFreq, default=TypeFreq)

    # asset
    assetMaxFreq = db.EmbeddedDocumentField(TypeFreq, default=TypeFreq)
    assetManualTask = db.EmbeddedDocumentField(TypePeriod, default=TypePeriod)

    # content
    contentMaxDepth = db.EmbeddedDocumentField(TypeFreq, default=TypeFreq)
    contentMaxFreq = db.EmbeddedDocumentField(TypeFreq, default=TypeFreq)
    contentManualTask = db.EmbeddedDocumentField(TypePeriod, default=TypePeriod)
    contentMaxPage = db.EmbeddedDocumentField(TypeFreq, default=TypeFreq)

    # ipv6
    ipv6ManualTask = db.EmbeddedDocumentField(TypePeriod, default=TypePeriod)

    # other
    maxPage = db.EmbeddedDocumentField(TypePeriod, default=TypePeriod)
    aviliableMaxHistory = db.EmbeddedDocumentField(TypeDuration, default=TypeDuration)
    whitelist = db.EmbeddedDocumentField(TypeLimit, default=TypeLimit)
    smsAlert = db.EmbeddedDocumentField(TypePeriod, default=TypePeriod)
    emailAlert = db.EmbeddedDocumentField(TypePeriod, default=TypePeriod)
    alertGroup = db.EmbeddedDocumentField(TypeLimit, default=TypeLimit)
    alertReceiver = db.EmbeddedDocumentField(TypeLimit, default=TypeLimit)
    maxHistory = db.EmbeddedDocumentField(TypeDuration, default=TypeDuration)
    urlBLSMaxNum = db.EmbeddedDocumentField(TypeLimit, default=TypeLimit)
    api = db.EmbeddedDocumentField(ExpTypeLimit, default=ExpTypeLimit)

    # 主机监测
    host = db.EmbeddedDocumentField(TypeLimit, default=TypeLimit)
    hostVulMaxFreq = db.EmbeddedDocumentField(TypeFreq, default=TypeFreq)
    hostVulManualTask = db.EmbeddedDocumentField(TypePeriod, default=TypePeriod)

    # web安全监测任务定时执行时间
    wsTaskTime = db.EmbeddedDocumentField(ExpTypeLimit, default=ExpTypeLimit)

    # 篡改监测
    changeCheckMaxFreq = db.EmbeddedDocumentField(TypeFreq, default=TypeFreq)   # 最大监测频率
    changeCheckMaxDepth = db.EmbeddedDocumentField(TypeFreq, default=TypeFreq)  # 最大监测深度
    changeCheckMaxPage = db.EmbeddedDocumentField(TypeFreq, default=TypeFreq)   # 最大监测URL数
    changeCheckMaxBaseChange = db.EmbeddedDocumentField(TypeFreq, default=TypeFreq)  # 样本更新最大频率， 根据任务执行次数
    changeCheckMaxKeyword = db.EmbeddedDocumentField(TypeFreq, default=TypeFreq)  # 自定义敏感词最大数量
    changeCheckMaxResource = db.EmbeddedDocumentField(TypeFreq, default=TypeFreq)  # 重点关注资源最大数量


class Balances(BaseDocument):
    """
    Args:
        db ([type]): [description]
    """
    uid = db.ObjectIdField(required=True)
    balance = db.EmbeddedDocumentField(Balance, default=Balance)
    feature = db.EmbeddedDocumentField(Feature, default=Feature)
    marks = db.StringField(default='')
    planChargeTime = db.DateTimeField(required=True)
    planExpireTime = db.DateTimeField(default=None)
    planId = db.ObjectIdField()

    meta = {
        'collection': 'balances',
        'strict': False
    }
